package ruler.rules.activities;

import java.util.List;

import ruler.main.R;
import ruler.main.WifiGPSRulerApplication;
import ruler.rules.Rule;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public abstract class RulesListActivity extends Activity {

	protected WifiGPSRulerApplication app;
	private List<Rule> enableRules;
	private List<Rule> disableRules;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rules_list);
		app = (WifiGPSRulerApplication) getApplication();
		initComponents();
	}

	public void onResume() {
		super.onResume();
		refreshLists();
	}

	private void initComponents() {
		enableRules = setEnableRules();
		disableRules = setDisableRules();

		ListView listViewEnable = (ListView) findViewById(R.id.listViewEnable);
		ListView listViewDisable = (ListView) findViewById(R.id.listViewDisable);

		listViewEnable.setAdapter(new RulesArrayAdapter(this, enableRules));
		listViewDisable.setAdapter(new RulesArrayAdapter(this, disableRules));

		AdapterView.OnItemClickListener onItemClicklistenerEnableRules = new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(RulesListActivity.this,
						RuleEditorActivity.class);
				i.putExtra("PARCEABLE_RULE", enableRules.get(position));
				startActivity(i);
			}
		};

		AdapterView.OnItemClickListener onItemClicklistenerDisableRules = new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(RulesListActivity.this,
						RuleEditorActivity.class);
				i.putExtra("PARCEABLE_RULE", disableRules.get(position));
				startActivity(i);
			}
		};

		listViewEnable.setOnItemClickListener(onItemClicklistenerEnableRules);
		listViewDisable.setOnItemClickListener(onItemClicklistenerDisableRules);
	}

	private void refreshLists() {
		enableRules = setEnableRules();
		disableRules = setDisableRules();
		ListView listViewEnable = (ListView) findViewById(R.id.listViewEnable);
		ListView listViewDisable = (ListView) findViewById(R.id.listViewDisable);
		listViewEnable.setAdapter(new RulesArrayAdapter(this, enableRules));
		listViewDisable.setAdapter(new RulesArrayAdapter(this, disableRules));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.newRule:
			Intent i = new Intent(RulesListActivity.this,
					RuleEditorActivity.class);
			startActivity(i);
			break;
		case R.id.deleteSelectedRules:
			deleteSelectedRules(enableRules);
			deleteSelectedRules(disableRules);
			refreshLists();
			app.saveRules();
			break;
		case R.id.restartService:
			app.restartService();
			break;
		case R.id.startService:
			app.startService();
			break;
		case R.id.stopService:
			app.stopService();
			break;
		}
		return true;
	}

	private void deleteSelectedRules(List<Rule> rules) {
		for (Rule rule : rules) {
			if (rule.isSelected()) {
				app.removeRule(rule);
			}
		}
	}

	protected abstract List<Rule> setEnableRules();

	protected abstract List<Rule> setDisableRules();

}
